package org.nfIt.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.nfIt.dto.system.LoginDto;
import org.nfIt.entity.system.SysUser;
import org.nfIt.service.SysMenuService;
import org.nfIt.service.SysUserService;
import org.nfIt.service.ValidateCodeVoService;
import org.nfIt.utils.AuthContextUtil;
import org.nfIt.vo.common.Result;
import org.nfIt.vo.common.ResultCodeEnum;
import org.nfIt.vo.system.LoginVo;
import org.nfIt.vo.system.SysMenuVo;
import org.nfIt.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClossName:IndexController
 * Pakage:org.nfIt.controller
 * Description
 *
 * @Tag(name = "用户接口") ：用于给控制器类打标签，以便于API文档（如Swagger）的分组和显示。
 * @Operation(summary = "登录接口"): 来自某个API文档框架（如Swagger）的注解，用于为该方法生成API文档时提供摘要信息。
 *
 * @Author L
 * @Create 2024/6/17 20:12
 * @Version 2023.1.3
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    /**
     * 用户service
     */
    @Autowired
    private SysUserService sysUserService;
    /**
     * 验证码
     */
    @Autowired
    private ValidateCodeVoService validateCodeVoService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 生成图片验证码
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeVoService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登入
     *
     * @param loginDto 用于封装登入提交过来表单的数据
     * # @RequestBody ： 注解告诉Spring将请求体中的JSON数据自动转换为LoginDto类型的对象
     */
    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出功能
     * 删除redis中存储的当前用户token中保存的用户信息
     *
     * @param token token
     */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

//    /**
//     * 获取当前登入用户信息
//     *
//     * @param token 生成的唯一标识
//     */
//    @GetMapping(value = "/getUserInfo")
//    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
//
//        SysUser sysUser = sysUserService.getUserInfo(token);
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }

    /**
     * 从threadLocal中获取登入信息,因为登入是已经把登入信息存入threadLocal
     * 获取当前登入用户信息
     *
     * @param token 登入生成的唯一标识
     */
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        //从threadLocal中获取登入信息
        AuthContextUtil authContextUtil = new AuthContextUtil();
        //从threadLocal获取登入用户信息
        SysUser sysUser = authContextUtil.get();
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户可以操作的菜单
     */
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }




}
